package com.holi.hiteacher.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.holi.hiteacher.R;
import com.holi.hiteacher.activity.TestActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @description:
 * @author: JoshuaWong E-mail:joshuawongchn@gmail.com
 * @date: 2016年12月23日 17:50
 * @version: 1.0
 */
public class MiiFragment extends Fragment {
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.lv_user_info)
    ListView listView;
    private List<Map<String, Object>> mData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mii, container, false);
        ButterKnife.bind(this, view);
        setHasOptionsMenu(true);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), TestActivity.class);
                startActivity(intent);
            }
        });

        mData = getData();
        MyAdapter adapter = new MyAdapter(getContext());
        listView.setAdapter(adapter);

        return view;
    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<>();

        Map<String, Object> map = new HashMap<>();
        map.put("imgUserInfo", R.drawable.img_timetable);
        map.put("tvUserInfo", "我的课程表");
        list.add(map);

        map = new HashMap<>();
        map.put("imgUserInfo", R.drawable.img_friend);
        map.put("tvUserInfo", "我的好友");
        list.add(map);

        map = new HashMap<>();
        map.put("imgUserInfo", R.drawable.img_signin);
        map.put("tvUserInfo", "我的签到");
        list.add(map);

        map = new HashMap<>();
        map.put("imgUserInfo", R.drawable.img_class_group);
        map.put("tvUserInfo", "我的班级群");
        list.add(map);

        map = new HashMap<>();
        map.put("imgUserInfo", R.drawable.img_plan);
        map.put("tvUserInfo", "学习计划");
        list.add(map);

        return list;
    }

    public final class ViewHolder {
        public ImageView img_user_info;
        public TextView tv_user_info;
    }

    public class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public MyAdapter(Context context) {
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int arg0) {
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_mii_info, null);
                holder.img_user_info = (ImageView) convertView.findViewById(R.id
                        .img_user_info);
                holder.tv_user_info = (TextView) convertView.findViewById(R.id.tv_user_info);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.img_user_info.setBackgroundResource((Integer) mData.get(position).get
                    ("imgUserInfo"));
            holder.tv_user_info.setText((String) mData.get(position).get("tvUserInfo"));
            return convertView;
        }
    }
}
